package com.googlecode.connectlet.crypto.rsa;

import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPublicKey;

import com.googlecode.connectlet.crypto.digest.SignatureStream;
import com.googlecode.connectlet.util.Bytes;

public abstract class RSASignatureStream extends SignatureStream {
	private static final long serialVersionUID = 1L;

	private RSAPublicKey publicKey = null;
	private RSAPrivateCrtKey privateKey = null;

	protected abstract byte[] getHeader();

	public RSASignatureStream(RSAPublicKey publicKey) {
		this.publicKey = publicKey;
	}

	public RSASignatureStream(RSAPrivateCrtKey privateKey) {
		this.privateKey = privateKey;
	}

	@Override
	public byte[] sign() {
		return RSA.sign(privateKey, Bytes.add(getHeader(), digest()));
	}

	@Override
	public boolean verify(byte[] sigBytes) {
		return Bytes.equals(Bytes.add(getHeader(), digest()), RSA.verify(publicKey, sigBytes));
	}
}